package com.atguigu.eduservice.service.impl;

import com.atguigu.com.exceptionhandler.GuliException;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2022-12-08
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduVideoService eduVideoService;
    @Override
    public List<ChapterVo> getChapterVideoByCoreseId(String courseId) {
        //根据id查询所有章节
        QueryWrapper<EduChapter> chapterWapper = new QueryWrapper<>();
        chapterWapper.eq("course_id",courseId);
        List<EduChapter> eduChapters = baseMapper.selectList(chapterWapper);
        //根据id查询所有小节
        QueryWrapper<EduVideo> videoWapper = new QueryWrapper<>();
        videoWapper.eq("course_id",courseId);
        List<EduVideo> eduVideos = eduVideoService.list(videoWapper);

        //封装所以章节
        List<ChapterVo> finalList = new ArrayList<>();

        for (int i = 0; i < eduChapters.size(); i++) {
            EduChapter eduChapter = eduChapters.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            finalList.add(chapterVo);

            //封装说有小节
            List<VideoVo> videoList = new ArrayList<>();
            //遍历list集合进行小节的封装
            for (int j = 0; j <eduVideos.size() ; j++) {
                EduVideo eduVideo = eduVideos.get(j);
                //判断 小节里的chapterid与章节的是否相等
                if (eduVideo.getChapterId().equals(eduChapter.getId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    //放到小节封装集合
                    videoList.add(videoVo);
                }

            }
            //把封装后的小节封装到章节中取
            chapterVo.setChildren(videoList);
        }

        return finalList;
    }

    @Override
    public boolean deleteChapterById(String chapterId) {
        //通过id判断里面有没有小节
        QueryWrapper<EduVideo> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("course_id",chapterId);
        int count = eduVideoService.count(videoQueryWrapper);
        if (count>0){
        //有小节
            throw new GuliException(20001,"有小节，无法删除");
        }
        int i = baseMapper.deleteById(chapterId);
        return i>0;
    }
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        baseMapper.delete(queryWrapper);
    }
}
